package controller

import (
	"context"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/go-pay/gopay/alipay"
	"github.com/go-pay/gopay/pkg/xlog"
)

type AlipayPayBaseController struct {
}

/**
 * 获取支付宝支付的client对象
 * @author feige
 * @date 2023-12-04
 * @version 1.0
 * @desc
 */
func (web *AlipayPayBaseController) GetAlipayPayClient() (*alipay.Client, context.Context) {
	//初始化支付宝客户端
	//    appId：应用ID
	//    privateKey：应用私钥，支持PKCS1和PKCS8
	//    isProd：是否是正式环境 false代表你可能沙箱模式
	appid := beego.AppConfig.DefaultString("alipay.pay.appid", "")
	returnUrl := beego.AppConfig.DefaultString("alipay.pay.returnUrl", "")
	notifyUrl := beego.AppConfig.DefaultString("alipay.pay.notifyUrl", "")
	privateKey := beego.AppConfig.DefaultString("alipay.pay.privatekey", "")

	ctx := context.Background()
	client, err := alipay.NewClient(appid, privateKey, true)
	if err != nil {
		xlog.Error(err)
		return nil, ctx
	}
	//配置公共参数
	client.SetCharset("utf-8").
		SetSignType(alipay.RSA2).
		SetReturnUrl(returnUrl).
		SetNotifyUrl(notifyUrl)
	return client, ctx
}
